﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AtletIS.Shared.Data.aDbDataSetTableAdapters;
using System.Data;
using AtletIS.Shared.Helpers;
using AtletIS.Shared.Entities;

namespace AtletIS.Shared.WpfControls
{
    public partial class SixAttemptsBox : UserControl
    {
        // Constructor
        public SixAttemptsBox(int nOrder, int nStartNumber, string sCountry, string sName, string sResult, int performanceId, DisciplineListEntity discipline)
        {
            InitializeComponent();

            if (nOrder == 0)
                txtOrder.Text = string.Empty;
            else
                txtOrder.Text = nOrder.ToString();

            string sFilePath = AppDomain.CurrentDomain.BaseDirectory;
            sFilePath = System.IO.Path.Combine(sFilePath, "Ext", "Flags", "24px", sCountry + ".png");
            if (System.IO.File.Exists(sFilePath))
            {
                BitmapImage flag = new BitmapImage();
                flag.BeginInit();
                flag.UriSource = new Uri(sFilePath);
                flag.EndInit();
                iCountry.Source = flag;
            }

            if (nStartNumber == 0)
                txtStartNumber.Text = string.Empty;
            else
                txtStartNumber.Text = "(" + nStartNumber.ToString() + ")";

            txtName.Text = sName;

            // Result
            txtResult.Text = sResult;

            // Attempts
            this.bor1.Visibility = System.Windows.Visibility.Hidden;
            this.bor2.Visibility = System.Windows.Visibility.Hidden;
            this.bor3.Visibility = System.Windows.Visibility.Hidden;
            this.bor4.Visibility = System.Windows.Visibility.Hidden;
            this.bor5.Visibility = System.Windows.Visibility.Hidden;
            this.bor6.Visibility = System.Windows.Visibility.Hidden;

            DataTable dtExtTexts = DatabaseHelper.GetExtTextsByPerformanceIdActive(performanceId); // taExtTexts.GetByPerfId(performanceId);

            #region Throws / Hor. jumps

            if (discipline.DisciplineCode == Globals.ThrowsCode | discipline.DisciplineCode == Globals.HorJumpCode)
            {
                // Attempt #1
                if (dtExtTexts.Select("[Header] = 1").Count() == 1)
                {
                    this.bor1.Visibility = System.Windows.Visibility.Visible;

                    DataRow drAtt = dtExtTexts.Select("[Header] = 1")[0];
                    this.txtAtt1.Text = PrintHelper.PrintResult(drAtt["Text1"].ToString(), discipline.DisciplineCode);
                    if (string.IsNullOrEmpty(drAtt["Text2"].ToString()))
                        this.txtHeader1.Visibility = System.Windows.Visibility.Collapsed;
                    else
                        this.txtHeader1.Text = drAtt["Text2"].ToString();
                }

                // Attempt #2
                if (dtExtTexts.Select("[Header] = 2").Count() == 1)
                {
                    this.bor2.Visibility = System.Windows.Visibility.Visible;

                    DataRow drAtt = dtExtTexts.Select("[Header] = 2")[0];
                    this.txtAtt2.Text = PrintHelper.PrintResult(drAtt["Text1"].ToString(), discipline.DisciplineCode);
                    if (string.IsNullOrEmpty(drAtt["Text2"].ToString()))
                        this.txtHeader2.Visibility = System.Windows.Visibility.Collapsed;
                    else
                        this.txtHeader2.Text = drAtt["Text2"].ToString();
                }

                // Attempt #3
                if (dtExtTexts.Select("[Header] = 3").Count() == 1)
                {
                    this.bor3.Visibility = System.Windows.Visibility.Visible;

                    DataRow drAtt = dtExtTexts.Select("[Header] = 3")[0];
                    this.txtAtt3.Text = PrintHelper.PrintResult(drAtt["Text1"].ToString(), discipline.DisciplineCode);
                    if (string.IsNullOrEmpty(drAtt["Text2"].ToString()))
                        this.txtHeader3.Visibility = System.Windows.Visibility.Collapsed;
                    else
                        this.txtHeader3.Text = drAtt["Text2"].ToString();
                }

                // Attempt #4
                if (dtExtTexts.Select("[Header] = 4").Count() == 1)
                {
                    this.bor4.Visibility = System.Windows.Visibility.Visible;

                    DataRow drAtt = dtExtTexts.Select("[Header] = 4")[0];
                    this.txtAtt4.Text = PrintHelper.PrintResult(drAtt["Text1"].ToString(), discipline.DisciplineCode);
                    if (string.IsNullOrEmpty(drAtt["Text2"].ToString()))
                        this.txtHeader4.Visibility = System.Windows.Visibility.Collapsed;
                    else
                        this.txtHeader4.Text = drAtt["Text2"].ToString();
                }

                // Attempt #5
                if (dtExtTexts.Select("[Header] = 5").Count() == 1)
                {
                    this.bor5.Visibility = System.Windows.Visibility.Visible;

                    DataRow drAtt = dtExtTexts.Select("[Header] = 5")[0];
                    this.txtAtt5.Text = PrintHelper.PrintResult(drAtt["Text1"].ToString(), discipline.DisciplineCode);
                    if (string.IsNullOrEmpty(drAtt["Text2"].ToString()))
                        this.txtHeader5.Visibility = System.Windows.Visibility.Collapsed;
                    else
                        this.txtHeader5.Text = drAtt["Text2"].ToString();
                }

                // Attempt #6
                if (dtExtTexts.Select("[Header] = 6").Count() == 1)
                {
                    this.bor6.Visibility = System.Windows.Visibility.Visible;

                    DataRow drAtt = dtExtTexts.Select("[Header] = 6")[0];
                    this.txtAtt6.Text = PrintHelper.PrintResult(drAtt["Text1"].ToString(), discipline.DisciplineCode);
                    if (string.IsNullOrEmpty(drAtt["Text2"].ToString()))
                        this.txtHeader6.Visibility = System.Windows.Visibility.Collapsed;
                    else
                        this.txtHeader6.Text = drAtt["Text2"].ToString();
                }
            }

            #endregion

            #region Ver. jumps

            if (discipline.DisciplineCode == Globals.VerJumpCode)
            {
                string[] heights = discipline.Params.Split(',');
                if (heights != null && heights.Length > 0)
                {
                    int from = 0, to = 0;

                    if (heights.Length <= 6)
                    {
                        from = 0;
                        to = 5;
                    }
                    else
                    {
                        from = heights.Length - 6;
                        to = heights.Length - 1;
                    }

                    int i = 1;
                    for (int index = from; index <= to; index++)
                    {
                        string filter = string.Format("Header = '{0}'", heights[index]);
                        if (dtExtTexts.Select(filter).Count() > 0)
                        {
                            Border b = null; TextBlock txtTop = null; TextBlock txtBottom = null;
                            switch (i)
                            {
                                case 1:
                                    b = bor1; txtTop = txtAtt1; txtBottom = txtHeader1;
                                    break;
                                case 2:
                                    b = bor2; txtTop = txtAtt2; txtBottom = txtHeader2;
                                    break;
                                case 3:
                                    b = bor3; txtTop = txtAtt3; txtBottom = txtHeader3;
                                    break;
                                case 4:
                                    b = bor4; txtTop = txtAtt4; txtBottom = txtHeader4;
                                    break;
                                case 5:
                                    b = bor5; txtTop = txtAtt5; txtBottom = txtHeader5;
                                    break;
                                case 6:
                                    b = bor6; txtTop = txtAtt6; txtBottom = txtHeader6;
                                    break;
                            }

                            b.Visibility = System.Windows.Visibility.Visible;
                            txtTop.Text = heights[index];
                            txtBottom.Text = dtExtTexts.Select(filter)[0]["Text1"].ToString();
                        }

                        i += 1;
                    }
                }
            }

            #endregion

        }









    }
}
